package Client.Dropbox.SystemCommands

import Client.Dropbox.Manager.FileManager
import Client.Dropbox.DropboxMain
import java.nio.file.Path
import java.nio.file.Files
import objects._
import Client.TaggingSystem.TagManager
import java.io.File

/**
 * Object that holds the commands that come in from the operating system
 */
object SystemCommand{
  val client=FileManager.client
  val user=FileManager.user
  val root=SystemListener.root
  
  /**
   * A file was changed in the Dropbox folder
   * @param file the file
   */
  def ChangedFile(file:Path) = {
    val size=Files.size(file)
    val sum=SumGenerator.createCheckSum(file)
    val name=file.getFileName.toString
    val parent=FileManager.get(root.relativize(file.getParent).toString)
    DropboxMain.communicator ! ("changedFile", new File2(user, client, name, parent, size, sum))
  }
  
  /**
   * A file was deleted in the Dropbox folder
   * @param file the file
   */
  def DeletedFile(file:Path) = {
    val name=file.getFileName.toString
    val parent=FileManager.get(root.relativize(file.getParent).toString)
    DropboxMain.communicator ! ("deletedFile", new File1(user, client, name, parent))
    TagManager.delete(file.toString)
  }
  
  /**
   * A folder was deleted in the Dropbox folder
   * @param folder the folder
   */
  def DeletedFolder(folder:Path) = {
    val name=root.relativize(folder).toString
    val parent=FileManager.get(root.relativize(folder.getParent).toString)
    DropboxMain.communicator ! ("deletedFolder", new File1(user, client, name, parent))
  }
  
  /**
   * A file was added in the Dropbox folder
   * @param file the file
   */
  def NewFile(file:Path) = {
    val size=Files.size(file)
    val sum=SumGenerator.createCheckSum(file)
    val name=file.getFileName.toString
    val parent=FileManager.get(SystemListener.root.relativize(file.getParent).toString)
    DropboxMain.communicator ! ("newFile", new File2(user, client, name, parent, size, sum))
    TagManager.tag(new File(file.toString))
  }
  
  /**
   * A folder was added in the Dropbox folder
   * @param folder the folder
   */
  def NewFolder(folder:Path) = {
    var name=folder.getFileName.toString
    var name2=root.relativize(folder).toString
    var parent=FileManager.get(root.relativize(folder.getParent).toString)
    DropboxMain.communicator ! ("newFolder", name2, new File1(user, client, name, parent))
    SystemListener.register(folder)
  }
}